package service

import (
	"ksd-social-api/commons/base/service"
	"ksd-social-api/commons/page"
	"ksd-social-api/modules/user/model"
	"ksd-social-api/modules/user/model/context"
	"ksd-social-api/modules/user/model/vo"
	"ksd-social-api/utils"
	"strconv"
)

type AdminUserService struct {
	service.BaseService
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  根据用户id查询用户信息
 */
func (service *AdminUserService) SaveUpdateUser(user model.User) (dbuser *model.User) {
	// 1: 根据用户id执行查询
	updateUser := adminUserMapper.SaveUpdateUser(user)
	return updateUser
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  根据用户id查询用户信息
 */
func (service *AdminUserService) GetUserById(id uint64, systemId uint64) (uvo *vo.UserVo) {
	// 1: 根据用户id执行查询
	user := adminUserMapper.GetUserByID(id, systemId)
	if user == nil {
		return nil
	}
	// 2: 把user的数据转换到uservo中返回
	userVo := vo.UserVo{}
	// 3: 把用户user复制到uservo中
	utils.CopyProperties(&userVo, user)
	// 4: 返回
	return &userVo
}

/**
 * @author feige
 * @date 2023-10-13
 * @version 1.0
 * @desc 查询资源列表信息，栏目和搜索使用
 */
func (service *AdminUserService) FindUserListAndPage(ctx context.UserSearchContext) *page.Page {

	p := adminUserMapper.FindUserListAndPage(ctx)
	users := p.Records.([]model.User)
	userVos := []vo.UserVo{}
	for _, user := range users {
		// 3: 查询当前登录的用户信息和资源id去查询用户资源收藏表
		userVo := vo.UserVo{}
		utils.CopyProperties(&userVo, user)
		// 2: 如果没有错误就必须把model数据导入vo数据中
		userVos = append(userVos, userVo)
	}

	p.Records = userVos
	return p
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  保存用户购买VIP信息
 */
func (service *AdminUserService) SaveUserBuyVip(ctx context.AdminUserVipContext) bool {
	userVipVo := userVipMapper.GetUserVipDetail(ctx.VipFlag.Uint64(), ctx.SystemId)
	userVo := userMapper.GetUserByID(ctx.UserId.Uint64(), ctx.SystemId)
	OutTradeNo := service.GetSnowWorkerIdString(6)
	var userBuyVip model.UserBuyVip
	userBuyVip.SystemId = ctx.SystemId
	userBuyVip.SystemName = ctx.SystemName
	// 处理返回结果
	userBuyVip.VipId = ctx.VipFlag.Uint64()
	userBuyVip.Title = userVipVo.Title
	userBuyVip.Description = userVipVo.Note

	userBuyVip.UserId = ctx.UserId.Uint64()
	userBuyVip.Username = userVo.Username
	userBuyVip.Avatar = userVo.Avatar
	userBuyVip.Uuid = userVo.Uuid
	userBuyVip.Phone = userVo.Telephone
	userBuyVip.Address = userVo.Address
	userBuyVip.Nickname = userVo.Nickname

	userBuyVip.Code = ""
	userBuyVip.PayMethod = 4
	userBuyVip.PayMethodName = "人工操作"
	userBuyVip.Tradeno = OutTradeNo
	userBuyVip.Orderno = OutTradeNo
	userBuyVip.OrderJson = ""
	userBuyVip.Price = userVipVo.Price
	// 保存订单的接口
	adminUserMapper.SaveUserBuyVip(userBuyVip)
	return true
}

func (service *AdminUserService) UpdateUserPassword(passwordContext context.AdminUserPasswordContext) (flag bool, err error) {
	// 使用新的uuid更新
	user := adminUserMapper.GetUserByID(passwordContext.UserId.Uint64(), passwordContext.SystemId)
	// 2: 获取用户唯一uuid和新密码进行加密
	if len(user.Slat) == 0 {
		user.Slat = utils.GetUUID()
	}
	return adminUserMapper.UpdatePwd(passwordContext.UserId.Uint64(), passwordContext.SystemId, user.Slat, utils.Md5Slat(passwordContext.Password, user.Slat))
}

/*
*
修改手机
*/
func (service *AdminUserService) UpdateUserTelephone(adminUserTelephoneContext context.AdminUserTelephoneContext) (flag bool, err error) {
	return adminUserMapper.UpdateAdminTelephone(adminUserTelephoneContext.UserId.Uint64(), adminUserTelephoneContext.SystemId, adminUserTelephoneContext.Telephone)
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  修改状态
 */
func (service *AdminUserService) UpdateFieldById(ctx context.UserUpdateContext) bool {
	return adminUserMapper.UpdateFieldById(ctx.Id.Uint64(), ctx.Field, ctx.Value, ctx.SystemId)
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  修改状态
 */
func (service *AdminUserService) UpdateFieldByCodeId(ctx context.UserUpdateContext) bool {
	return adminUserMapper.UpdateFieldByCodeId(ctx.Id.Uint64(), ctx.Field, ctx.Value, ctx.SystemId)
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  保存和修改兑换码
 */
func (service *AdminUserService) SaveUpdateCode(ctx context.UserCodeContext) *vo.UserCodeVO {
	// 保存入库
	var userCode model.UserVipCode
	var userVoCode vo.UserCodeVO
	utils.CopyProperties(&userCode, ctx)
	parseUint, _ := strconv.ParseUint(ctx.Id, 10, 64)
	userCode.Id = parseUint
	userCode.Opid = ctx.Opid.Uint64()
	data := adminUserMapper.SaveUpdateUserCode(userCode)
	utils.CopyProperties(&userVoCode, data)
	return &userVoCode
}

/**
 * @author feige
 * @date 2023-10-13
 * @version 1.0
 * @desc 查看兑换码
 */
func (service *AdminUserService) FindCodePage(ctx context.CodeSearchContext) *page.Page {
	p := adminUserMapper.FindUserCodePage(ctx)
	userCodes := p.Records.([]model.UserVipCode)
	userCodeVos := []vo.UserCodeVO{}
	for _, userCode := range userCodes {
		// 3: 查询当前登录的用户信息和资源id去查询用户资源收藏表
		userCodeVo := vo.UserCodeVO{}
		utils.CopyProperties(&userCodeVo, userCode)
		// 2: 如果没有错误就必须把model数据导入vo数据中
		userCodeVos = append(userCodeVos, userCodeVo)
	}
	p.Records = userCodeVos
	return p
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  根据id删除
 */
func (service *AdminUserService) DelById(id uint64, systemId uint64) bool {
	return adminUserMapper.DelById(id, systemId)
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  根据用户id查询用户明细
 */
func (service *AdminUserService) DelCodeById(id uint64, systemId uint64) bool {
	return adminUserMapper.DelCodeById(id, systemId)
}
